<template>
  <!-- // 分配角色 -->
  <div>
    <div>
      <el-checkbox-group v-model="roleIds">
        <el-checkbox
          v-for="item in rolesList"
          :key="item.id"
          :label="item.id"
        >{{ item.name }}</el-checkbox>
      </el-checkbox-group>
    </div>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="doSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getUserDetailById } from '@/api/user'
import { updatedRoles } from '@/api/permissions'
export default {
  props: {
    curId: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      // 选中的角色id
      roleIds: [],
      // 获取的角色数据
      rolesList: []
    }
  },
  created() {
    // 1.1获取角色数据
    this.loadRoles()
    // 1.2获取角色已经有的权限
    this.loadRolesById()
  },
  // 2方法
  methods: {
    // 2.1取消按钮
    closeDialog() {

    },
    // 2.1获取角色数据
    async  loadRoles() {
      const res = await getRoles({ size: 1, pagesize: 1000 })
      this.rolesList = res.data.rows
    },
    // 2.2获取角色已经有的权限
    async loadRolesById() {
      const res = await getUserDetailById(this.curId)
      this.roleIds = res.data.roleIds
    },
    // 2.3确定按钮
    async doSubmit() {
      await updatedRoles(this.curId, this.roleIds)
      this.$message.success('分配权限成功')
      this.$emit('success')
    }
  }
}
</script>
